package com.example.myrecycyleview528zyr.model;

import com.example.myrecycyleview528zyr.bean.ArticleBean;
import com.example.myrecycyleview528zyr.iface.ArticleListener;
import com.example.myrecycyleview528zyr.iface.Articleiface;
import com.example.myrecycyleview528zyr.service.ArticleService;

import java.util.List;
import java.util.concurrent.Callable;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static retrofit2.converter.gson.GsonConverterFactory.*;

/**
 * Created by jone on 2017/5/22.
 */

public class ArticleModel implements Articleiface {
    private Retrofit retrofit;

    public ArticleModel() {
        String BASEURL = "http://amicool.neusoft.edu.cn/";
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, final ArticleListener listener) {
        ArticleService articleservice = retrofit.create(ArticleService.class);
        Call<List<ArticleBean>> call = articleservice.getArticleList(mod, page, sessionID);
        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("FAIL");
                }
            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}

